//
//  TerperatureAppDelegate.m
//  Terperature
//
//  Created by Aaron Cordova on 10/3/08.
//  Copyright __MyCompanyName__ 2008. All rights reserved.
//

#import "TerperatureAppDelegate.h"


@implementation TerperatureAppDelegate

@synthesize window;
@synthesize picker;

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    
    buildingsArray = [[NSArray arrayWithObjects: 
                      @"A.V. Williams Building",@"Adele H. Stamp Student Union Building",@"Agriculture Shed",
                      @"Allegany Hall (Dorm)",@"Animal Science Service Building",
                      @"Animal Science/Agricultural Engineering Building",@"Annapolis Hall",
                      @"Anne Arundel Hall (Dorm)",@"Apiary",@"Aquatics Center",@"Architecture Building",
                      @"Art-Sociology Building",@"Baltimore Hall (Dorm)",@"Bel Air Hall",@"Benjamin Building",
                      @"Biology-Psychology Building",@"Biosciences Research Bldg",@"Blacksmith Shop",
                      @"Building Services Operations Building",@"Byrd Stadium Building (Upper Deck)",
                      @"Calvert Hall (Dorm)",@"Cambridge Community Center",@"Cambridge Hall (Dorm)",
                      @"Campus Mail Facility",@"Caroline Hall (Dorm)",@"Caroll Hall (Dorm)",@"Cattle Barn",
                      @"Cecil Hall (Dorm)",@"Center for Young Children",@"Central Animal Resources Facility",
                      @"Centreville Hall (Dorm)",@"Charles Hall (Dorm)",@"Chemical and Nuclear Engineering Building",
                      @"Chemistry Building",@"Chesapeake Building",@"Chestertown Hall (Dorm)",
                      @"Clarice Smith Performing Arts Center",@"Cole Student Activities Building",
                      @"Comcast Center",@"Computer and Space Sciences Building",
                      @"Computer Science Instructional Center",@"Cumberland Hall (Dorm)",@"Denton Hall (Dorm)",
                      @"Dorchester Hall (Dorm)",@"Easton Hall (Dorm)",@"Education Annex,@West",@"Elkton Hall (Dorm)",
                      @"Ellicot Hall",@"Energy Research Facility",@"Engineering Annex",
                      @"Engineering Laboratory Building",@"Francis Scott Key Hall",@"Frederick Hall (Dorm)",
                      @"Garrett Hall (Dorm)",@"Geology Building",@"H.J. Patterson Hall",@"Hagerstown Hall (Dorm)",
                      @"Harford Hall (Dorm)",@"Harrison Lab (Green Houses)",@"Health Center",
                      @"Holzapfel Hall (Horticulture)",@"Hornbake Library (Undergraduate)",@"Horse Barn",
                      @"Howard Hall (Dorm)",@"Indoor Practice Facility",
                      @"Institute for Physical Science and Technology",@"Instructional Television Facility",
                      @"J.H. Kehoe Track & Ludwig Field",@"J.M. Patterson Building",
                      @"Jeong H. Kim Engineering Building",@"Jimenez Hall",@"Journalism Building",@"Jull Hall",
                      @"Kent Hall (Dorm)",@"LaPlata Hall (Dorm)",@"Lee Building",@"LeFrak Hall",
                      @"Main Administration Building",@"Manufacturing Building",@"Marie Mount Hall",@"Martin Hall",
                      @"Mathematics Building",@"McKeldin Library",@"Memorial Chapel",@"Microbiology Building",
                      @"Mitchell Building",@"Montgomery Hall (Dorm)",@"Morrill Hall",@"Motor Transportation Facility",
                      @"Mowatt Lane Substation",@"Neutral Buoyancy Research Facility",@"Nyumburu Cultural Center",
                      @"Patuxent Building",@"Physics Building",@"Plant Science Building",@"Pocomoke Building",
                      @"Potomac Building",@"Preinkert Field House",@"President's Residence",
                      @"Prince Georges Hall (Dorm)",@"Queen Anne's Hall",@"Reckord Armory",
                      @"Recycling Center",@"Rossborough Inn (F/S Alumni Club)",@"Sheep Barn",
                      @"Shipley Field House (Baseball)",@"Shoemaker Building",@"Shriver Laboratory",
                      @"Shuttle Bus Facility",@"Skinner Building",@"Somerset Hall (Dorm)",@"South Campus Dining Hall",
                      @"St. Mary's Hall (Dorm)",@"Stadium Drive Parking Garage (SDG)",@"Susquehanna Hall",
                      @"Symons Hall",@"Talbot Hall (Dorm)",@"Taliaferro Hall",@"Tawes Fine Arts Building",
                      @"Technology Advancement Building",@"Terrapin Trail (Arena) Parking Garage",
                      @"Track/Soccer Ticket Booth",@"Turner Hall (Dairy/Visitor Center)",@"Tydings Hall",
                       @"Tyser Tower",@"UMUC/Inn & Conference Center (ICC)",@"UMUC/Student & Faculty Services Center",
                       @"Van Munching Hall",@"Varsity Sports Teamhouse",@"Washington Hall (Dorm)",
                       @"Wicomico Hall (Dorm)",@"Wind Tunnel Building",@"Woods Hall",@"Worcester Hall (Dorm)", 
                       nil] retain];
    
    locManager = [[[CLLocationManager alloc] init] autorelease];
    locManager.delegate = self;
    locManager.distanceFilter = 10;
    locManager.desiredAccuracy =  kCLLocationAccuracyKilometer; // kCLLocationAccuracyNearestTenMeters;
    //if(!locManager.locationServicesEnabled) {
    //    NSLog(@"location services are disabled");
    //}
    //else {
        NSLog(@"updating location ...");
        [locManager startUpdatingLocation];
    //}
}



- (void)dealloc {
    [window release];
    [picker release];
    [buildingsArray release];
    [super dealloc];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return [buildingsArray count];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return 280.0;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [buildingsArray objectAtIndex:row];
}


- (CGFloat)pickerView:(UIPickerView *)pickerViewRowHeightForComponent:(NSInteger)component {
    return 40.0;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component  {
    currentBuilding = [buildingsArray objectAtIndex:row];
}

- (IBAction)reportHot:(id)sender {
    NSString *atitle;
    NSString *amessage;
    
    NSURLRequest *reportReq = [NSURLRequest requestWithURL:
                               [NSURL URLWithString:                                
                                CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, 
                                                                        [NSString stringWithFormat:@"http://terperature.appspot.com/entry?building=%@&room=none&temp=1", currentBuilding]
                                                                        , NULL, NULL, kCFStringEncodingUTF8)]];
    
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:reportReq delegate:self];
    if(conn) {
        //NSMutableData *receivedData=[[NSMutableData data] retain];
        atitle = @"Thanks";
        amessage = @"for participating.\n The app will now exit.";
    }
    else {
        atitle = @"Sorry";
        amessage = @"We could not complete your request. \n Check your network settings and try again";
    }
    
    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:atitle message:amessage delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
    [alertView release];
    
}

- (IBAction)reportCold:(id)sender {
    
    NSString *atitle;
    NSString *amessage;
    
    NSURLRequest *reportReq = [NSURLRequest requestWithURL:
                               [NSURL URLWithString:                                
                                 CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, 
                                                                         [NSString stringWithFormat:@"http://terperature.appspot.com/entry?building=%@&room=none&temp=-1", currentBuilding]
                                                                         , NULL, NULL, kCFStringEncodingUTF8)]];
    
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:reportReq delegate:self];
    if(conn) {
        //NSMutableData *receivedData=[[NSMutableData data] retain];
        atitle = @"Thanks";
        amessage = @"for participating.\n The app will now exit.";
    }
    else {
        atitle = @"Sorry";
        amessage = @"We could not complete your request. \n Check your network settings and try again";
    }
    
    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:atitle message:amessage delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
    [alertView release];
}

// exit after user clicks OK on an alert
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {    
    UIApplication *app = [UIApplication sharedApplication];
    SEL selector = @selector(terminate);
    
    if( [app respondsToSelector:selector] ) {
        [app performSelector:selector];
    }    
}    



@end

